/*
 * 07文件格式化读写操作.c
 *
 *  Created on: 2024年3月17日
 *      Author: HP
 */
#include<stdio.h>
#include<string.h>
#include<stdlib.h>
#include<math.h>
#include<time.h>

int main1007(){
	FILE* fp = fopen("D:/zsl/a.txt", "r");
	if(!fp){
		return -1;
	}
	char* p = (char*)malloc(sizeof(char)*100);

	int a;
	// 格式化输出
	fscanf(fp, "%3d", &a);
	printf("%d\n", a);
	// 遇到空格、回车结束
	fscanf(fp, "%s", p);
	printf("%s\n", p);
	fscanf(fp, "%s", p);
	printf("%s\n", p);
	free(p);
	fclose(fp);
	return 0;
}

int main100701(){
	FILE* fp = fopen("D:/zsl/四则运算结果.txt", "r");
	if(!fp){
		return -1;
	}
	int a,b,c;
	// 格式化读取
	fscanf(fp, "%d-%d=%d", &a, &b, &c);
	printf("%d\n", a);
	printf("%d\n", b);
	printf("%d\n", c);
	fclose(fp);
	return 0;
}

int main100702(){
	srand((size_t)time(NULL));
	FILE* fp = fopen("D:/zsl/四则运算2.txt", "w");
	if(!fp){
		return -1;
	}
	int a,b;
	char c;
	for(int i=0; i<100; i++){
		a = rand()%10+1;
		b = rand()%10+1;
		switch(rand()%4){
		case 0: c='+'; break;
		case 1: c='-'; break;
		case 2: c='*'; break;
		case 3: c='/'; break;
		}
		fprintf(fp, "%d%c%d=\n", a, c, b);
	}
	fclose(fp);
	return 0;
}
